<?php
/**
 * 微信红包管理
 */
namespace Common\Model;
use Think\Exception;

class RedpackModel extends BaseModel{
	static $recordInstance=null;
	public static function getRecordInstance(){
		if(!self::$recordInstance){
			self::$recordInstance=M('redpack_record',null,DB_MAIN_CFG);
		}
		return self::$recordInstance;
	}



	/**
	 * 获取红包细分记录
	 * @param $beginTime 	开始时间戳
	 * @param $endTime 	结束时间戳
	 * @param $page
	 * @param $pageSize
	 * @param int $uid
	 * @param int $setId
	 * @return array
	 */
	public static function getRecordList($beginTime,$endTime,$page,$pageSize,$uid=0,$setId=0){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');
		$tradeM = self::getRecordInstance();

		$where='1';

		if ($uid>0){
			$where .=" and uid={$uid}";
		}

		if ($setId>0){
			$where .=" and setId={$setId}";
		}

		if($beginTime>0){
			$where .=" and sendTime >={$beginTime}";
		}
		if($endTime>0){
			$where .=" and sendTime <={$endTime}";
		}


		$where .=" and uid>0 and sendTime>0";


		$dataSuccess['total'] = $tradeM->where($where)->count();//获取数据总条数
		$dataSuccess['page'] = $page ? $page : 1;//返回当前页
		$dataSuccess['totalPage'] = ceil($dataSuccess['total']/$pageSize);//总共页数
		$dataSuccess['nextPage'] = $dataSuccess['page'] >= $dataSuccess['totalPage'] ? 0 : 1;//是有有下一页
		$offset=($dataSuccess['page']-1)*$pageSize;


		$exchangeList = $tradeM->where($where)
			->order('id desc')
			->limit($offset,$pageSize)
			->select();

		$dataSuccess['data']=$exchangeList;
		return $dataSuccess;
	}




	/////////////////////////生成红包分配方案//////////////////////////////////////////////////////
	public static function checkAssign($totalMoney,$num,$minMoney){
		return $minMoney*$num<=$totalMoney;
	}

	//资讯添加或修改
	public static function createRecord($gid,$money,$num,$minMoney){
		$recordData=array();
		$setArray=array($minMoney,$money,$num);
		$rt=self::calcuteAssign($money,$setArray);

		if($rt['status']!=0){
			return array('status'=>1,'msg'=>$rt['msg']);
		}else{
			$time=time();
			foreach($rt['data'] as $packValue){
				$recordData[]=array(
					'setId'=>$gid,
					'money'=>$packValue,
					'addTime'=>$time
				);
			}
		}

		shuffle($recordData);

		self::getRecordInstance()->addAll($recordData);
		return array('status'=>0,'msg'=>'success');

	}


	/**
	 * 计算分配方案
	 * 金额以分为单位
	 * @param $money 分配金额
	 * @param $setArray 分配方案数组
	 * @return array
	 */
	private static function calcuteAssign($money,$setArray){

			$min=floatval($setArray[0]);
			$total=floatval($setArray[1]);
			$num=intval($setArray[2]);

			$minMoney = bcmul($min , $num);

			if($min==$total){
				$left=0;
			}else{
				$left=bcsub($total,bcmul($min,$num));
			}

		if($minMoney > $money){//不够分
			return array('status'=>1,'msg'=>'金额不足，最低金额要求'.($minMoney/100).'元');
		}elseif ($minMoney == $money){//刚好分完
			$assign=array_fill(0,$num,$min);
			return array('status'=>0,'data'=>$assign );
		}


		$assign	= array_fill(0,$num,$min);
		$left	= $left*100/$num;
		for($i=0;$i<$num;$i++){
			$changeValue=rand(0,$left);//变化量
			$assign[$i] += $changeValue/100;
		}

		$left = $total-array_sum($assign);
		if($left>0){
			$left*=100;
			for($i=0;$i<$num;$i++){
				$changeValue=rand(0,$left);//变化量
				$left -=$changeValue;
				$assign[$i] += $changeValue/100;
			}

			if ($left>0){
				$assign[rand(0,$num-1)] +=$left/100;
			}
		}

		return array('status'=>0,'data'=>$assign);
	}







}